package com.asset.dispoal.extension.strategy;

import com.asset.dispoal.extension.domain.dto.RolePermissionDTO;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.strategy.AbstractStrategyManager;
import tech.mhuang.pacebox.core.strategy.BaseStrategyManager;
import tech.mhuang.pacebox.core.util.ObjectUtil;

import javax.annotation.PostConstruct;
import java.util.Map;

@Component
public class DataPowerStrategyManager extends AbstractStrategyManager<RolePermissionDTO, String> implements BaseStrategyManager<RolePermissionDTO, String>, ApplicationContextAware {

    private ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @PostConstruct
    @Override
    public void init() {
        Map<String, BaseDataPowerStrategyServer> type = applicationContext.getBeansOfType(BaseDataPowerStrategyServer.class);
        for (Map.Entry<String, BaseDataPowerStrategyServer> entry : type.entrySet()) {
            BaseDataPowerStrategyServer caseTypeStrategyServer = entry.getValue();
            if (ObjectUtil.isEmpty(caseTypeStrategyServer.event())) {
                continue;
            }
            caseTypeStrategyServer.event().forEach(event -> set(event, caseTypeStrategyServer));
        }
    }
}
